<template>
  <div class="resourceSharing">
    <div class="top_img" :style="'background-image: url(' + topBg + ')'">
      <span>资 源 共 享</span>
    </div>
    <div class="main">
      <div class="left">
        <div class="title">资源共享</div>
      </div>
      <div class="right">
        <div class="title">
          <span>
            <el-input
              placeholder="请输入内容"
              v-model="query.key.title"
              clearable
            >
            </el-input>
            <el-button @click="getLearnDatumList">搜索</el-button>
            <el-button @click="clikcUploadResources">上传资源</el-button>
          </span>
          <span>
            <span @click="$router.push('/')">首页</span>>><span>资源共享</span>
          </span>
        </div>
        <ul>
          <li
            v-for="(e, i) in initData"
            :key="i"
            @click="$router.push('/resource_sharing/details/' + e.id)"
          >
            <div class="list_left">
              <div class="col_title">
                {{ e.title }}
              </div>
              <div class="col_content">
                <span>
                  {{ e.createdBy }}
                </span>
                <span>
                  {{
                    $moment(parseInt(e.createdAt)).format("YYYY-MM-DD HH:mm:ss")
                  }}
                </span>
              </div>
            </div>
            <div class="list_right">
              <el-button
                type="text"
                @click.stop="clickDownload(e.datumNameUrl)"
              >
                下载文件
              </el-button>
            </div>
          </li>
        </ul>
      </div>
    </div>
    <el-pagination
      style="text-align: center; margin-top: 24px"
      background
      layout="prev, pager, next"
      :total="total"
      @current-change="handleSizeChange"
      :page-size="query.pageSize"
    >
    </el-pagination>

    <el-dialog title="上传资源" :visible.sync="dialogVisible" width="420px">
      <div class="main_dialog">
        <div class="col_box">
          <div class="col_title">上传资料：</div>

          <el-upload
            class="upload-demo"
            :show-file-list="false"
            action="#"
            :http-request="imgUpload"
            :on-success="handleAvatarSuccess"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
          <span
            @click="clikcDatumName"
            style="margin-left: 12px; cursor: pointer"
            v-show="
              queryData.datumNameUrl != '' || queryData.datumNameUrl != null
            "
          >
            {{ queryData.datumName }}
          </span>
        </div>
        <div class="col_box">
          <div class="col_title">标题：</div>
          <el-input
            v-model="queryData.title"
            placeholder="请输入内容"
          ></el-input>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="clickCancel">取 消</el-button>
        <el-button type="primary" @click="clickSave"> 确 定 </el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getLearnDatumList,
  getAddLearnDatum,
  uploadImageLearnDatum,
} from "@/util/learnDatum.js";

export default {
  data() {
    return {
      topBg:
        "http://175.178.92.128:8087/campus_help_system/images/insidePages/20190910163919740.jpg",
      query: {
        key: {
          title: "",
          checked: 1,
        },
        pageSize: 5,
        pageNum: 1,
      },
      initData: [],
      total: 5,
      dialogVisible: false,
      queryData: {
        title: "",
        createdAt: "",
        userId: "",
        createdBy: "",
        checked: 0,
        datumName: "",
        datumNameUrl: "",
      },
    };
  },
  created() {
    this.getLearnDatumList();
  },
  mounted() {},
  methods: {
    // 获取数据
    async getLearnDatumList() {
      let { data: res } = await getLearnDatumList(this.query);
      if (res.status != 2000) {
        return this.$message.error(res.msg);
      }
      this.initData = res.data.list;
      this.total = res.data.total;
    },
    // 点击下载
    clickDownload(e) {
      window.open(e, "_blank");
    },
    // 分页
    handleSizeChange(e) {
      this.query.pageNum = e;
      this.getLearnDatumList();
    },
    // 上传图片
    async imgUpload(e) {
      var fm = new FormData();
      fm.append("coverFile", e.file);
      let { data: res } = await uploadImageLearnDatum(fm);
      console.log(res);
      this.queryData.datumName = res.data.datumName;
    },
    // 上传图片 成功的回调
    handleAvatarSuccess(res, file) {
      console.log(file);
      this.queryData.datumNameUrl = URL.createObjectURL(file.raw);
    },
    // 点击上传文件
    clikcDatumName() {
      window.open(this.queryData.datumNameUrl, "_blank");
    },
    // 点击按钮 上传资源
    clikcUploadResources() {
      if (
        window.localStorage.getItem("id") == "" ||
        window.localStorage.getItem("id") == null
      ) {
        return this.$message.error("请先登录");
      }
      this.dialogVisible = true;
    },
    // 点击按钮保存
    async clickSave() {
      // 判空
      if (this.isAddData()) {
        return;
      }
      // 本地获取作者 新增
      this.queryData.createdBy = window.localStorage.getItem("userName");
      this.queryData.userId = window.localStorage.getItem("id");
      // 默认审核状态
      this.queryData.checked = 0;
      let { data: res } = await getAddLearnDatum(this.queryData);
      if (res.status != 2000) {
        return this.$message.error(res.msg);
      }
      this.$message.success(res.msg);
      this.clickCancel();
    },
    // 点击取消
    clickCancel() {
      this.queryData.datumName = "";
      this.queryData.title = "";
      this.dialogVisible = false;
    }, //判空
    isAddData() {
      if (this.queryData.datumName == "" || this.queryData.datumName == null) {
        this.$message.error("文件不能为空");
        return true;
      }
      if (this.queryData.title == "" || this.queryData.title == null) {
        this.$message.error("标题不能为空");
        return true;
      }
      return false;
    },
  },
};
</script>
<style lang="less" scoped>
.resourceSharing {
  padding-bottom: 50px;
  .top_img {
    height: 280px;
    background-repeat: no-repeat;
    background-size: auto 100%;
    background-position: center center;
    display: flex;
    align-items: center;
    justify-content: center;
    & > span {
      font-size: 48px;
      font-weight: 700;
      color: #fff;
    }
  }
  .main {
    width: 1200px;
    margin: 0 auto;
    padding-top: 50px;
    display: flex;
    .left {
      margin-right: 60px;
      .title {
        height: 50px;
        font-size: 18px;
        background: #0166b6;
        padding-left: 15px;
        width: 200px;
        line-height: 50px;
        color: #fff;
        margin-bottom: 24px;
      }
    }

    .right {
      flex: 1;
      .title {
        justify-content: space-between;
        display: flex;
        padding: 0 0 12px 0;
        border-bottom: 1px solid #ddd;
        & > span:nth-child(1) {
          display: flex;
          .el-button {
            margin-left: 12px;
          }
        }
        & > span:nth-child(2) {
          & > span:nth-child(1) {
            cursor: pointer;
            margin-right: 12px;
          }
          & > span:nth-child(2) {
            color: #0e3782;
            margin-left: 12px;
          }
        }
      }

      & > ul {
        & > li {
          cursor: pointer;
          padding: 18px 0;
          border-bottom: 1px solid #e5e5e5;
          display: flex;
          align-items: center;
          justify-content: space-between;
          .list_left {
            .col_title {
              line-height: 26px;
              font-size: 16px;
              color: #666666;
              width: 300px;
              white-space: nowrap; //在一行显示
              text-overflow: ellipsis; //多余用....显示
              overflow: hidden;
              margin-bottom: 12px;
            }
            .col_content {
              span {
                color: #aaa;
                font-size: 14px;
                margin-right: 12px;
              }
            }
          }
          // .list_right {
          // }
        }
      }
    }
  }

  .main_dialog {
    .col_box {
      display: flex;
      align-items: center;
      margin-bottom: 24px;
      .col_title {
        width: 90px;
        text-align: right;
        margin-right: 8px;
      }
      .el-input,
      .el-textarea {
        width: 230px;
      }
    }
  }
}
</style>
